package com.ruoyi.employment.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.employment.domain.TAssist;
import com.ruoyi.employment.domain.THangOver;
import com.ruoyi.employment.dto.AssistDto;
import com.ruoyi.employment.mapper.TAssistMapper;
import com.ruoyi.employment.service.ITAssistService;
import com.ruoyi.employment.service.ITHangOverService;
import com.ruoyi.teacher.api.TeacherService;
import com.ruoyi.teacher.domain.TTeacher;
import com.ruoyi.dto.EmailDto;
import com.ruoyi.utils.AESUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.utils.EmailUtil;
import com.ruoyi.utils.SendRobotMessageUtil;

import java.util.Date;
import java.util.List;

/**
 * 就业帮扶Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-12-24
 */
@Service
@Slf4j
public class TAssistServiceImpl implements ITAssistService 
{
    @Autowired
    private TAssistMapper tAssistMapper;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private ITHangOverService tHangOverservice;
    @Autowired
    private EmailUtil emailUtil;
    /**
     * 查询就业帮扶
     * 
     * @param id 就业帮扶主键
     * @return 就业帮扶
     */
    @Override
    public TAssist selectTAssistById(Long id)
    {
        return tAssistMapper.selectTAssistById(id);
    }

    /**
     * 查询就业帮扶列表
     * 
     * @param tAssist 就业帮扶
     * @return 就业帮扶
     */
    @Override
    public List<TAssist> selectTAssistList(TAssist tAssist)
    {
        String username = SecurityUtils.getUsername();
        if (username.equals("admin")) {
            username = null;
        }
        tAssist.setTeacher(username);
        return tAssistMapper.selectTAssistList(tAssist);
    }

    /**
     * 新增就业帮扶
     * 
     * @param tAssist 就业帮扶
     * @return 结果
     */
    @Override
    public int insertTAssist(TAssist tAssist)
    {
        AjaxResult teacherByName = teacherService.findTeacherByName(tAssist.getTeacher());
        if (teacherByName.isError()) {
            throw new ServiceException("网络异常");
        }
        Object data = teacherByName.get("data");
        TTeacher tTeacher = JSONObject.parseObject(JSONObject.toJSONString(data), TTeacher.class);
        try {
            SendRobotMessageUtil.helpNotice(tAssist.getTeacher(), tTeacher.getPhone());
        } catch (Exception e) {
            throw new ServiceException("网络异常");
        }
        tAssist.setState(0L);
        return tAssistMapper.insertTAssist(tAssist);
    }

    /**
     * 修改就业帮扶
     * 
     * @param tAssist 就业帮扶
     * @return 结果
     */
    @Override
    public int updateTAssist(TAssist tAssist)
    {
        return tAssistMapper.updateTAssist(tAssist);
    }

    /**
     * 批量删除就业帮扶
     * 
     * @param ids 需要删除的就业帮扶主键
     * @return 结果
     */
    @Override
    public int deleteTAssistByIds(Long[] ids)
    {
        return tAssistMapper.deleteTAssistByIds(ids);
    }

    /**
     * 删除就业帮扶信息
     * 
     * @param id 就业帮扶主键
     * @return 结果
     */
    @Override
    public int deleteTAssistById(Long id)
    {
        return tAssistMapper.deleteTAssistById(id);
    }

    /**
     * 移交就业帮扶
     * @param assistDto
     * @return
     */
    @Override
    public String handOver(AssistDto assistDto) {
        TAssist tAssist = selectTAssistById(assistDto.getAssistId());
        if (tAssist == null) {
            throw new ServiceException("当前帮扶任务不存在", -1);
        }
        if (tAssist.getState() == -1) {
            throw new ServiceException("当前任务已移交, 请勿重复移交");
        }
        // 设置帮扶任务状态为-1，已移交
        tAssist.setState(-1L);
        updateTAssist(tAssist);
        // 调用老师api通过老师名字查询老师所有信息
        AjaxResult teacherByName = teacherService.findTeacherByName(assistDto.getTeacher());
        if (teacherByName.isError()) {
            throw new ServiceException("操作失败", -1);
        }
        Object teacher = teacherByName.get("data");
        TTeacher tTeacher = JSONObject.parseObject(JSONObject.toJSONString(teacher), TTeacher.class);
        if (assistDto.getSend().equals(1)) {
            try {
                SendRobotMessageUtil.helpNotice(tTeacher.getName(), tTeacher.getPhone());
            } catch (Exception e) {
                throw new ServiceException("运行异常", -1);
            }
            log.info("宁收到一个帮扶移交任务:{}" ,tTeacher.getPhone());
        }
        // 将此帮扶任务修改并交给目标老师
        THangOver tHangOver = new THangOver();
        tHangOver.setHandOverPerson(tAssist.getTeacher());
        tHangOver.setInheritor(tTeacher.getName());
        tHangOver.setHandTime(new Date());
        tHangOver.setTaskName(tAssist.getProblemType());
        tHangOver.setNote(assistDto.getNote());
        tHangOverservice.insertTHangOver(tHangOver);

        tAssist.setState(0L);
        tAssist.setTeacher(tTeacher.getName());
        tAssist.setId(null);
        insertTAssist(tAssist);
        return "移交成功";
    }

    @Override
    public String assistFinish(Long id) {
        TAssist tAssist = selectTAssistById(id);
        if (tAssist == null) {
            throw new ServiceException("当前帮扶任务不存在");
        }
        tAssist.setState(1L);
        updateTAssist(tAssist);
        EmailDto emailDto = new EmailDto();
        String code = AESUtil.encrypt(tAssist.getId().toString(), AESUtil.MY_KEY);
        System.out.println(code);
        emailDto.setSendTo(tAssist.getEmail());
        emailDto.setSubject("宁的帮扶任务已完成");
        emailDto.setContent(tAssist.getStudentName() + "宁好，对于[" + tAssist.getBookedTime() + "]" + "的问题是否得到解决," +
                "请点击" + "http://localhost/evaluate?code="  + code + " 对此次服务进行评价。");
        emailUtil.sendMessage(emailDto);
        return "已完成帮扶任务";
    }

    /**
     * 根据code获取帮扶任务详情
     * @param code
     * @return
     */
    @Override
    public TAssist getAssist(String code) {
        // 对传过来的code 进行解密
        System.out.println(code);
        String s = AESUtil.decrypt(code, AESUtil.MY_KEY);
        Long assisId = Long.valueOf(s);
        // 用解密出的id查询帮扶任务
        TAssist tAssist = selectTAssistById(assisId);
        if (tAssist == null) {
            throw new ServiceException("当前帮扶任务不存在");
        }
        return tAssist;
    }
}
